//
//  ShippingViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/25.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftyJSON

protocol ShippingViewControllerDelegate {
    func giveTheShippingDataToProcessOrderVC(_ id:String,title:String,cost:String)
}

class ShippingViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var delegate : ShippingViewControllerDelegate?
    
    var dataArr = NSMutableArray()
    var myTableView = UITableView(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-64), style: .grouped)
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.creatNavView()
        self.getData()
        self.creatTableView()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
extension ShippingViewController{
    fileprivate func creatNavView(){
        self.view.backgroundColor = UIColor.white
        self.automaticallyAdjustsScrollViewInsets = false
        
        self.title = "SHIPPING METHOD"
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setImage(UIImage(named: "nav_back"), for: UIControlState())
        leftBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), for: .touchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
        
    }
    
    func leftBtnClick(){
        self.navigationController?.popViewController(animated: true)
    }
    
}

extension ShippingViewController{
    fileprivate func getData(){
        let urlStr = APIBASE + "main_page=shipping_method"
        
        let parma = ["zenid":UserSession.shareInstance.cookie!]
        NetworkTool.shareInstance.request(.post, urlString: urlStr, parmas: parma as [String : NSObject]) { (result, error) in
            if result != nil{
                let json = JSON(result!)
                print(json)
                
                let arr = (result as! NSDictionary)["data"]as?[[String:NSObject]]
                for dic in arr!{
                    self.dataArr.add(dic)
                }
                
                self.myTableView.reloadData()
            }else{
                SVProgressHUD.showError(withStatus: "get cart data error:" + String(describing: error))
                print("get cart data error:" + String(describing: error))
            }
        }
        
    }
}

extension ShippingViewController{
    fileprivate func creatTableView(){
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        }
        let dic = self.dataArr[indexPath.row]as?NSDictionary
        
        cell?.textLabel?.text = dic!["title"] as? String
        cell?.detailTextLabel?.text = dic!["cost"] as? String
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT*0.1)
        headerView.backgroundColor = UIColor.white
        
        let label = UILabel()
        label.frame = CGRect(x: 20, y: 5, width: SCREEN_WIDTH-40, height: SCREEN_HEIGHT*0.1-10)
        label.text = "SHIPMENT METHOD:\n(It can make fastest arrive)"
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 14)
        headerView.addSubview(label)
        
        
        return headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return SCREEN_HEIGHT*0.1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return SCREEN_HEIGHT*0.08
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dic = self.dataArr[indexPath.row]as?NSDictionary
        
        self.delegate!.giveTheShippingDataToProcessOrderVC((dic!["id"]as?String)!, title: (dic!["title"]as?String)!, cost: (dic!["cost"]as?String)!)
        self.navigationController?.popViewController(animated: true)
    }
    
}













